import { fileURLToPath, URL } from "node:url";
import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";
import vueDevTools from "vite-plugin-vue-devtools";
import path from "path";
// https://vite.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd(), "");
  console.log(env.VITE_API_URL);
  return {
    plugins: [vue(), vueJsx(), vueDevTools()],
    resolve: {
      alias: {
        "@": fileURLToPath(new URL("./src", import.meta.url)),
      },
    },
    base: process.env.VITE_API_URL,
    server: {
      port: 5173,
      open: true,
      proxy: {
        [env.VITE_API_URL]: {
          target: "http://localhost:3001",
          changeOrigin: true,
          rewrite: (path) => {
            // 只有当路径以 /api 开头时才进行重写
            if (/^\/api/.test(path)) {
              return path.replace(/^\/api/, "");
            } else {
              // 对于不符合条件的路径，返回 undefined 或 "" 来阻止代理
              return undefined; // 或者 ""
            }
          },
        },
      },
    },
  };
});
